package com.mikgonz.farkle.database;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseOpenHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "farkle_db.db";
	private static final int DATABASE_VERSION = 1;
	public static final	String CHAT_SUFFIX = "_chat";

	private Dao<Friend, String> friendDao = null;
	private Dao<Request, String> requestDao = null;
	private Dao<Message, String> messagesDao = null;

	public DatabaseOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
		try {
			TableUtils.createTable(arg1, Friend.class);
			TableUtils.createTable(arg1, Request.class);
			TableUtils.createTable(arg1, Message.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
			int arg3) {

	}

	public Dao<Friend, String> getFriendDao() throws SQLException {
		if (friendDao == null)
			friendDao = getDao(Friend.class);
		return friendDao;
	}

	public Dao<Request, String> getRequestDao() throws SQLException {
		if (requestDao == null)
			requestDao = getDao(Request.class);
		return requestDao;
	}
	
	public Dao<Message, String> getMessagesDao() throws SQLException {
		if (messagesDao == null)
			messagesDao = getDao(Message.class);
		return messagesDao;
		
	}

}
